package ducas.getprice.demo;

import com.dukascopy.api.Instrument;
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.system.IClient;
import com.dukascopy.api.system.ISystemListener;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This small program demonstrates how to initialize Dukascopy client and start
 * a strategy
 */
public class Main {

    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
    //url of the DEMO jnlp
    private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    private static String userName = "DEMO2cgwuu";
    private static String password = "cgwuu";

    public static String getJnlpUrl() {
        return jnlpUrl;
    }

    public static String getPassword() {
        return password;
    }

    public static String getUserName() {
        return userName;
    }

    public static void setJnlpUrl(String jnlpUrl) {
        Main.jnlpUrl = jnlpUrl;
    }

    public static void setPassword(String password) {
        Main.password = password;
    }

    public static void setUserName(String userName) {
        Main.userName = userName;
    }

    public static void main(String[] args) throws Exception {
        //get the instance of the IClient interface
        final IClient client = ClientFactory.getDefaultInstance();
        //set the listener that will receive system events
        client.setSystemListener(new ISystemListener() {

            private int lightReconnects = 3;

            @Override
            public void onStart(long processId) {
                LOGGER.info("Strategy started: " + processId);
            }

            @Override
            public void onStop(long processId) {
                LOGGER.info("Strategy stopped: " + processId);
                if (client.getStartedStrategies().isEmpty()) {
                    System.exit(0);
                }
            }

            @Override
            public void onConnect() {
                LOGGER.info("Connected");
                lightReconnects = 3;
            }

            @Override
            public void onDisconnect() {
                LOGGER.warn("Disconnected");
                if (lightReconnects > 0) {
                    client.reconnect();
                    --lightReconnects;
                } else {
                    try {
                        //sleep for 10 seconds before attempting to reconnect
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        //ignore
                    }
                    try {
                        client.connect(jnlpUrl, userName, password);
                    } catch (Exception e) {
                        LOGGER.error(e.getMessage(), e);
                    }
                }
            }
        });

        LOGGER.info("Connecting...");
        //connect to the server using jnlp, user name and password
        client.connect(jnlpUrl, userName, password);

        //wait for it to connect
        int i = 10; //wait max ten seconds
        while (i > 0 && !client.isConnected()) {
            Thread.sleep(1000);
            i--;
        }
        if (!client.isConnected()) {
            LOGGER.error("Failed to connect Dukascopy servers");
            System.exit(1);
        }

        //subscribe to the instruments
        Set<Instrument> instruments = new HashSet<Instrument>();
//        Added by tinhcx
        instruments.add(Instrument.XAUUSD);
        instruments.add(Instrument.EURUSD);
        instruments.add(Instrument.GBPUSD);
        instruments.add(Instrument.AUDUSD);
        instruments.add(Instrument.USDCAD);
        instruments.add(Instrument.USDCHF);
        instruments.add(Instrument.USDJPY);
//        
        LOGGER.info("Subscribing instruments...");
        client.setSubscribedInstruments(instruments);
        //start the strategy
        LOGGER.info("Starting strategy");
        client.startStrategy(new //<editor-fold defaultstate="collapsed" desc="comment">
                MA_Play //</editor-fold>
                ());
        //now it's running
    }
}
